//
//  HttpRequsetApi.m
//  NomalTest
//
//  Created by 陈小东 on 15/6/18.
//  Copyright (c) 2015年 &#26477;&#24030;&#26102;&#25237;&#20449;&#24687;&#31185;&#25216;@&#38472;&#23567;&#19996;. All rights reserved.
//

#import "HttpRequsetApi.h"
#import "BaseObject.h"
#import <UtilsMacro.h>
#import <AFNetworking.h>

#define kApiKey                     @"2c9e52fe965e3ec1b513d9b11dfb7bd8"
#define kLotteryListUrl             @"http://apis.baidu.com/apistore/lottery/lotterylist"
#define kLotteryQueryUrl            @"http://apis.baidu.com/apistore/lottery/lotteryquery"

@implementation HttpRequsetApi

+ (void)fetchLotteryList{
    NSString *urlStr = @"http://chart.cp.360.cn/kaijiang/kaijiang?lotId=166406&spanType=2&span=2015-06-17_2015-06-17";
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url
                                                               cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                           timeoutInterval:20];
    [request setHTTPMethod:@"GET"];
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               NSString *responseString = [[NSString alloc] initWithData:data
                                                                                encoding:NSASCIIStringEncoding];
                               NSMutableArray *array1 = [NSMutableArray arrayWithArray:[responseString componentsSeparatedByString:@"class='gray'>"]];
                               [array1 removeObjectAtIndex:0];
                               NSMutableArray *array2 = [NSMutableArray arrayWithArray:[[self stringWithArray:array1] componentsSeparatedByString:@"</td></tr><tr><td"]];
                               [array2 removeLastObject];
                               NSMutableArray *array3 = [NSMutableArray arrayWithArray:[[self stringWithArray:array2] componentsSeparatedByString:@"/td><td class='blue'><em class='orange'"]];
                               NSMutableArray *array4 = [NSMutableArray arrayWithArray:[[self stringWithArray:array3] componentsSeparatedByString:@"/em> <em class='blue'"]];
                               NSMutableArray *array5 = [NSMutableArray arrayWithArray:[[self stringWithArray:array4] componentsSeparatedByString:@"/em></td><td"]];
                               NSLog(@"%@",array5);
                           }];
}

+ (NSString *)stringWithArray:(NSArray *)array{
    NSMutableString *str1 = [[NSMutableString alloc] init];
    for(NSString *str2 in array){
        [str1 appendString:str2];
    }
    return str1;
}

+ (void)fetchLotteryQuery:(NSString *)lotterycodeCode
                  success:(SUCCESS_BLOCK)success
                  failure:(FAIL_BLOCK)failure{
    NSURL *url = [NSURL URLWithString:kFormat(@"%@?lotterycode=%@&recordcnt=1",kLotteryQueryUrl,lotterycodeCode)];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url
                                                               cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                           timeoutInterval:10];
    [request setHTTPMethod:@"GET"];
    [request addValue:kApiKey forHTTPHeaderField:@"apikey"];
    
    AFHTTPRequestOperation *requstOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    requstOperation.responseSerializer = [AFJSONResponseSerializer serializer];
    requstOperation.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    [requstOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        if([responseObject isKindOfClass:[NSDictionary class]]){
            NSDictionary *retData = responseObject[@"retData"];
            id data = retData[@"data"];
            NSString *lotteryCode = retData[@"lotteryCode"];
            if ([data isKindOfClass:[NSArray class]]) {
                data = (NSArray *)data[0];
            }
            BaseObject *resultObj = [[LotteryTicketItem alloc]initWithDictionary:data];
            ((LotteryTicketItem *)resultObj).lotteryCode = lotteryCode;
            if(success){
                success(resultObj);
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    [[AFHTTPRequestOperationManager manager].operationQueue addOperation:requstOperation];
}

@end
